package com.org.core.utils;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil {
	private final static String REG_RULE ="[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？]";
	
    public static String mapStringToString(Map<String,String> map){
    	StringBuffer sb = null;
    	if(map != null && map.size() > 0){
    		sb = new StringBuffer();
    		Iterator<Entry<String,String>>  it = map.entrySet().iterator();
    		while(it.hasNext()){
    			Entry<String,String> entity = it.next();
    			sb.append(entity.getKey()+ "=" +entity.getValue().toString()+" ");
    		}
    	}
    	return sb.toString();
    }
	public  static String getUUIDValue(){
		return  UUID.randomUUID().toString()+System.currentTimeMillis();
	}
	
    
    public static String getIp(){
    	String ip = "";
		try {
			InetAddress address = InetAddress.getLocalHost();
			ip = address.getHostAddress();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}   
    	return ip;
    }
    
    public static String simpleClassName(Class<?> clazz) {
        if (clazz == null) {
            return "null_class";
        }

        Package pkg = clazz.getPackage();
        if (pkg != null) {
            return clazz.getName().substring(pkg.getName().length() + 1);
        } else {
            return clazz.getName();
        }
    }

	public static String toEntityName(String fieldName, boolean toUpper) {
		String newFieldName = "";
		String[] fieldNameAry = fieldName.split("");
		for (int i = 0; i < fieldNameAry.length; i++) {
			String fieldNameTmp = fieldNameAry[i];
			if(fieldNameTmp.equals("_")){
				i++;
				fieldNameTmp = fieldNameAry[i];
				if(toUpper){
					fieldNameTmp = fieldNameTmp.toUpperCase();
				}
			}
			newFieldName += fieldNameTmp;
		}

		return newFieldName;
	}

	/**
	 * TODO 这个可以用正则表达式匹配来实现
	 * @param fieldName
	 * @return
	 */
	public static String toDbName(String fieldName) {
		StringBuffer newFieldName = new StringBuffer();
		char cTmp;
		for (int i = 0; i < fieldName.length(); i++) {
			cTmp = fieldName.charAt(i);
			if(!Character.isLowerCase(cTmp)) {
				newFieldName.append("_").append(Character.toLowerCase(cTmp));
			} else {
				newFieldName.append(cTmp);
			}
		}
		return newFieldName.toString();
	}
	
	/**
	 * 获取get方法
	 * @param fieldname
	 * @return
	 */
	public static String toGetter(String fieldname) {
        if (fieldname == null || fieldname.length() == 0) {
            return null;
        }
        /* If the second char is upper, make 'get' + field name as getter name. For example, eBlog -> geteBlog */
        if (fieldname.length() > 2) {
            String second = fieldname.substring(1, 2);
            if (second.equals(second.toUpperCase())) {
                return new StringBuffer("get").append(fieldname).toString();
            }
        }
        fieldname = new StringBuffer("get").append(fieldname.substring(0, 1).toUpperCase())
                .append(fieldname.substring(1)).toString();
        return  fieldname;
    }
	
    public static Long getTimestamp(){
    	return new Date().getTime()/1000;
    } 
    
    public static boolean notSafe(String content){
    	Pattern   p   =   Pattern.compile(REG_RULE);
    	Matcher   m   =   p.matcher(content); 
    	return m.find(); 
    }
    
    public static StringBuffer removeLastChar(StringBuffer sb){
    	return sb.replace(sb.length()-1, sb.length(), "");
    }
    
    /**
     * 去除最后N个字符
     * @param sb
     * @param n
     * @return
     */
    public static StringBuffer removeLastNChar(StringBuffer sb, int n){
    	return sb.replace(sb.length()-n, sb.length(), "");
    }
}
